$musicDir = '/home/djmary/music'; ////////////////////////// header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Last-Modified: '.gmdate('D, d M Y H:i:s'). ' GMT'); header('Cache-Control: no-store, no-cache, must-revalidate'); header('Cache-Control: pre-check=0, post-check=0, max-age=0'); $correct = array(); $now = mktime(); // scan music directory for directories if(($dh = opendir($musicDir))) { while(($file = readdir($dh)) !== false) { $path = $musicDir . '/' . $file; if(filetype($path) == 'dir') { // check for expected format: Y-M-D Y-M-D pass if(preg_match('/^(\d{4})-(\d{2})-(\d{2}) (\d{4})-(\d{2})-(\d{2}) (.*)$/', $file, $m)) { // unpack list(, $y1, $m1, $d1, $y2, $m2, $d2, $pass) = $m; // check whether current time is between given dates $from = mktime(0, 0, 0, $m1, $d1, $y1); $until = mktime(23, 59, 60, $m2, $d2, $y2); if($now >= $from && $now < $until) $correct[$path] = $pass; } } } } // bail if there is no current music if(empty($correct)) { header('Content-type: text/plain; charset=utf-8'); echo 'Most épp nincs semmilyen zene, gyere vissza később!'; exit; } // check for mp3 request $reqCnt = false; $reqName = false; if(preg_match('#^/get/(\d+)/([^/]+\.mp3)$#', $_SERVER['PATH_INFO'], $m)) { $reqCnt = (int) $m[1]; $reqName = urldecode($m[2]); if(strstr($reqName, '/')) { $reqName = false; $reqCnt = false; } } // check in files $found = false; $cntr = 0; foreach($correct as $path => $pass) { if($reqName) { // test if we have the file to dl $fp = $path . '/' . $reqName; if($reqCnt == $cntr && file_exists($fp)) { // demand authorization if(!isset($_SERVER['PHP_AUTH_USER']) || $pass != $_SERVER['PHP_AUTH_PW']) { header('WWW-Authenticate: Basic realm="djmary"'); header('HTTP/1.0 401 Unauthorized'); header('Content-type: text/plain; charset=utf-8'); echo 'Jelszót megmondtam az órán; a felhasználónév mindegy'; exit; } header('Content-type: application/octet-stream'); header('Content-length: ' . filesize($fp)); readfile($fp); exit; } } else { // list contents if(!$found) { echo '